/*
 * Copyright 2004-2010, Thorbjrn Lindeijer <thorbjorn@lindeijer.nl>
 * Copyright 2004-2006, Adam Turk <aturk@biggeruniverse.com>
 *
 * This file is part of libtiled-java.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package tiled.core;

import java.awt.Image;
import java.util.Properties;

/**
 * The core class for our tiles.
 */
public class Tile {
	private Image		image;
	private int			id	= -1;
	private Properties	properties;
	private TileSet		tileset;

	public Tile() {
		this.properties = new Properties();
	}

	public Tile(TileSet set) {
		this();
		this.setTileSet(set);
	}

	/**
	 * Copy constructor
	 * 
	 * @param t
	 *            tile to copy
	 */
	public Tile(Tile t) {
		this.properties = (Properties) t.properties.clone();
		this.tileset = t.tileset;
	}

	/**
	 * Sets the id of the tile as long as it is at least 0.
	 * 
	 * @param i
	 *            The id of the tile
	 */
	public void setId(int i) {
		if (i >= 0) {
			this.id = i;
		}
	}

	/**
	 * Sets the image of the tile.
	 * 
	 * @param i
	 *            the new image of the tile
	 */
	public void setImage(Image i) {
		this.image = i;
	}

	/**
	 * Sets the parent tileset for a tile.
	 * 
	 * @param set
	 */
	public void setTileSet(TileSet set) {
		this.tileset = set;
	}

	public void setProperties(Properties p) {
		this.properties = p;
	}

	public Properties getProperties() {
		return this.properties;
	}

	/**
	 * Returns the tile id of this tile, relative to tileset.
	 * 
	 * @return id
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * Returns the {@link tiled.core.TileSet} that this tile is part of.
	 * 
	 * @return TileSet
	 */
	public TileSet getTileSet() {
		return this.tileset;
	}

	public int getWidth() {
		if (this.image != null) {
			return this.image.getWidth(null);
		}
		return 0;
	}

	public int getHeight() {
		if (this.image != null) {
			return this.image.getHeight(null);
		}
		return 0;
	}

	/**
	 * Returns the tile image for this Tile.
	 * 
	 * @return Image
	 */
	public Image getImage() {
		return this.image;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Tile " + this.id + " (" + this.getWidth() + "x" + this.getHeight() + ")";
	}
}
